<script setup>
import router from "@/router/index.js";
import {reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import request from "@/utils/request.js";

const formRef = ref()

const data = reactive({
  user: JSON.parse(localStorage.getItem('code_user')),
  form: {},
  rules: {
    password: [
      {required: true, message: '请输入原密码', trigger: 'blur'},
    ],
    newPassword: [
      {required: true, message: '请输入新密码', trigger: 'blur'},
    ],
    confirmPassword: [
      {required: true, message: '请输入确认密码', trigger: 'blur'},
    ]
  }
})

const updatePassword = () => {
  formRef.value.validate(valid => {
    if (valid) { //验证通过的情况下
      request.post('/user/updatePassword', data.user).then(res => {
        if (res.code === '200'){
          ElMessage.success("修改成功")
          setInterval(() => {
            //清除缓存，推出到login
            localStorage.removeItem('code_user')
            location.href = '/'
          },500)
        }else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}


</script>

<template>
  <div>
    <!--    返回-->
    <div style="display: flex;align-items: center;flex-direction: row;margin-top: 10px"
         @click="router.push('/home')">
      <el-icon size="large"><Back /></el-icon>
      <span style="margin-left: 5px">返回</span>
    </div>

    <div class="card" style="width: 100%;margin-top: 20px">
      <div style="font-size: 16px;">修改密码</div>
      <el-form ref="formRef" :rules="data.rules" :model="data.user" label-width="80px" style="padding: 20px 30px 10px 0">
        <el-form-item prop="password" label="原密码">
          <el-input show-password v-model="data.user.originalPassword" autocomplete="off" prefix-icon="Lock"
                    placeholder="请输入原密码"/>
        </el-form-item>
        <el-form-item prop="newPassword" label="新密码">
          <el-input show-password v-model="data.user.newPassword" autocomplete="off" prefix-icon="Lock"
                    placeholder="请输入新密码"/>
        </el-form-item>
        <el-form-item prop="confirmPassword" label="确认密码">
          <el-input show-password v-model="data.user.confirmPassword" autocomplete="off" prefix-icon="Lock"
                    placeholder="请确认密码"/>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" style="padding: 20px 35px" @click="updatePassword">保存</el-button>
      </div>
    </div>

  </div>
</template>

<style scoped>
.custom-upload :deep(.el-upload-list__item) {
  width: 80px !important;
  height: 80px !important;
  margin: 5px;
}
.custom-upload :deep(.el-upload-list) {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  max-width: 300px;
  overflow: hidden;
}
</style>